package com.scau.controller;

import com.scau.common.exception.BusinessException;
import com.scau.common.exception.StatusException;
import com.scau.common.message.ResponseMessage;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionController {

    @ExceptionHandler(StatusException.class)
    public ResponseMessage status(StatusException statusException) {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setMessage(statusException.getMessage());
        responseMessage.setStatus(false);
        responseMessage.setCode(statusException.getCode());
        return responseMessage;
    }

    @ExceptionHandler(BusinessException.class)
    public ResponseMessage businessException(BusinessException businessException) {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setMessage(businessException.getMessage());
        responseMessage.setStatus(false);
        return responseMessage;
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseMessage runtimeException(RuntimeException runtimeException) {
        runtimeException.printStackTrace();
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setMessage(runtimeException.getMessage());
        responseMessage.setStatus(false);
        return responseMessage;
    }
}
